package com.test.cordova.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.test.cordova.R;


/**
 * 自定义下载进度弹窗
 */

public class DownloadDialog extends Dialog {

    private Context context;
    private ProgressBar download_tip_progressbar;
    private TextView download_tip_progress;

    public DownloadDialog(@NonNull Context context) {
        this(context,android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth);
    }

    public DownloadDialog(@NonNull Context context, int themeResId) {
        super(context,themeResId);
        this.context = context;
        init();
    }


    private void init() {
        View inflate = LayoutInflater.from(context).inflate(R.layout.download_tip_layout, null, false);
        download_tip_progress = inflate.findViewById(R.id.download_tip_progress);
        download_tip_progressbar = inflate.findViewById(R.id.download_tip_progressbar);
        download_tip_progress.setText(context.getString(R.string.android_auto_update_download_progress, 0));
        download_tip_progressbar.setMax(100);
        download_tip_progressbar.setProgress(0);
        setContentView(inflate);
        //点击对话框外面,对话框不消失
        setCanceledOnTouchOutside(false);
        setOnKeyListener(keylistener);
        setCancelable(false);
    }

    private static OnKeyListener keylistener = new OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                return true;
            } else {
                return false;
            }
        }
    };

    /**
     * 更新进度
     * @param progressTip
     */
    public void setProgressTip(int progressTip){
        if (download_tip_progress != null) {
            download_tip_progress.setText(context.getString(R.string.android_auto_update_download_progress, progressTip));
            download_tip_progressbar.setProgress(progressTip);
        }

    }

}
